package com.wantong.jianpu.customviews;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.BindingAdapter;

import com.wantong.jianpu.R;

import me.goldze.mvvmhabit.utils.StringUtils;

/**
 * @author PACY
 * @date 2022/4/25
 * description:
 */
public class CommTextView extends LinearLayout {
    String content;
    String title;
    int color;

    TextView contentTv;
    TextView titleTv;

    public CommTextView(Context context) {
        super(context);
    }

    public CommTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        /**
         * 加载布局  获取自定义属性
         */
        LayoutInflater.from(context).inflate(R.layout.comm_text_view, this, true);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CommTextView);
        content = a.getString(R.styleable.CommTextView_commTextContent);
        title = a.getString(R.styleable.CommTextView_commTextTitle);
        color=a.getColor(R.styleable.CommTextView_commTextContentColor, ContextCompat.getColor(context,R.color.fontColor));
        a.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        contentTv = findViewById(R.id.content_tv);
        titleTv=findViewById(R.id.title_tv);
        if (StringUtils.isNotEmpty(content)) {
            setContent(content);
            contentTv.setTextColor(color);
        }
        if (StringUtils.isNotEmpty(title)) {
            setTitleText(title);
        }
    }

    /**
     * 设置内容
     */
    public void setContent(String content) {
        contentTv.setText(content);

    }

    /**
     * 设置hint
     */
    public void setTitleText(String title) {
        titleTv.setText(title);
    }



    @BindingAdapter("commText_Content")
    public static void setStrContent(CommTextView topSelectView, String content) {
        if (topSelectView != null) {
            String mCurrentStr = topSelectView.contentTv.getText().toString().trim();
            if (StringUtils.isNotEmpty(content)) {
                if (!content.equalsIgnoreCase(mCurrentStr)) {
                    topSelectView.contentTv.setText(content);
                }
            }
        }
    }
}
